import Cookies from 'js-cookie'
import {getDomainLogo} from "@/api/system";

const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false
  },
  device: 'desktop',
  brandInfo: {
    simpleName: '教管壹',
    homeLogo: 'https://jiaoguanyi008.oss-cn-shenzhen.aliyuncs.com/jgy/homeLog.png',
    domainName: 'www.jiaoguanyi.cn',
    icp: '粤ICP备17133107号',
    companyName: '深圳市七彩宏云智能设备有限公司'
  }
}

const mutations = {
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SETBRANDINFO: (state, info) => {
    state.brandInfo = info
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  setBrandInfo({ commit }, domain) {
    getDomainLogo({domain_name: domain}).then(res => {
      if (res.data) {
        let info = {
          simpleName: res.data.simple_name,
          homeLogo: res.data.home_logo,
          domainName: res.data.domain_name,
          icp: res.data.icp,
          companyName: res.data.company_name
        }
        commit('SETBRANDINFO', info)
      }
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
